package cn.com.wangluotongxin.member.controller;

import cn.com.wangluotongxin.common.web.AjaxResult;
import cn.com.wangluotongxin.member.constants.Constants;
import cn.com.wangluotongxin.member.form.MemberCheckPhoneForm;
import cn.com.wangluotongxin.member.form.MemberInfoAuthForm;
import cn.com.wangluotongxin.member.form.MemberInfoRegisterForm;
import cn.com.wangluotongxin.member.form.MemberInfoUpdateForm;
import cn.com.wangluotongxin.member.service.IMemberInfoService;
import cn.com.wangluotongxin.member.vo.MemberCheckPhoneVo;
import cn.com.wangluotongxin.redis.service.RedisService;
import cn.com.wangluotongxin.web.utils.AuthUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 网络通信-用户信息 前端控制器
 * </p>
 *
 * @author AutoGenerator
 * @since 2024-11-28
 */
@Api(tags = "网络通信-用户信息 相关接口")
@RestController
@RequestMapping("/member/info")
public class MemberInfoController {

    @Autowired
    private IMemberInfoService memberInfoService;

    @Autowired
    private RedisService redisService;

    @ApiOperation("注册")
    @PostMapping("/register")
    public AjaxResult register(@RequestBody MemberInfoRegisterForm form) {
        Assert.isTrue(StrUtil.isNotBlank(form.getPhone()), "缺少手机号");
        Assert.isTrue(StrUtil.isNotBlank(form.getPassword()), "缺少密码");
        Assert.isTrue(redisService.setNx(Constants.MEMBER_INFO_REGISTER_KEY.concat(form.getPhone()), form.getPhone(), 3), "请不要重复点击");

        memberInfoService.register(form);
        return AjaxResult.success("注册成功");
    }

    @ApiOperation("用户信息更新")
    @PostMapping("/update")
    public AjaxResult updateInfo(@RequestBody MemberInfoUpdateForm form) {

        memberInfoService.updateInfo(form, AuthUtil.getLoginUser());
        return AjaxResult.success();
    }

    @ApiOperation("实名认证")
    @PostMapping("/auth")
    public AjaxResult auth(@RequestBody MemberInfoAuthForm form) {
        Assert.isTrue(StrUtil.isNotBlank(form.getName()), "缺少姓名");
        Assert.isTrue(StrUtil.isNotBlank(form.getIdCard()), "缺少身份证号");
        Assert.isTrue(IdcardUtil.isValidCard(form.getIdCard()), "身份证号格式错误");

        memberInfoService.memberAuth(form, AuthUtil.getLoginUser().getId());
        return AjaxResult.success();
    }

    @ApiOperation("效验手机号是否有账号")
    @PostMapping("/check/phone")
    public AjaxResult<MemberCheckPhoneVo> checkPhone(@RequestBody MemberCheckPhoneForm form) {
        Assert.isTrue(StrUtil.isNotBlank(form.getPhone()), "缺少手机号");

        return AjaxResult.success(memberInfoService.checkPhone(form.getPhone()));
    }
}